﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Description;

using GameServerLibrary.DBServer;
namespace GameServerLibrary
{
    public delegate void GetDBServerStatusCallback(int DBServerId,int DBServerStatus);

    public class GameServer
    {
        private DBServerClient _DBServerClient;
        private GetDBServerStatusCallback _GetDBServerStatusCallback;
        private string _DBServerUrl;
        public void Start(string DBServerUrl)
        {
            ConnectDBServer(DBServerUrl);
        }

        public void GetDBServerStatus(GetDBServerStatusCallback Callback)
        {
            _GetDBServerStatusCallback = Callback;
            int DBServerStatus = -1;
            try
            {
                _DBServerClient.BeginGetDBServerStatus(GetDBServerStatusCallback, DBServerStatus);
            }
            catch (Exception ex)
            {
            }
        }

        public StatusReport GetStatusReport()
        {
            return null;
        }

        private void GetDBServerStatusCallback(IAsyncResult ar)
        {
            int DBServerStatus = -1;
            try
            {
                DBServerStatus = _DBServerClient.EndGetDBServerStatus(ar);
            }
            catch (Exception ex)
            {

            }
            if (_GetDBServerStatusCallback != null)
            {
                _GetDBServerStatusCallback(0, DBServerStatus);
            }
        }

        /// <summary>
        /// 创建与数据库服务器的连接
        /// </summary>
        /// <param name="DBServerUrl"></param>
        private void ConnectDBServer( string DBServerUrl)
        {
            _DBServerUrl = DBServerUrl;
            //创建与数据库服务器的连接
            EndpointAddress epAddress = new EndpointAddress(DBServerUrl);
            BasicHttpBinding Binding = new BasicHttpBinding();
            _DBServerClient = new DBServerClient(Binding, epAddress);
            _DBServerClient.Open(); 
        }

    }
}
